class Machine < ActiveRecord::Base
	has_many :pdms
	belongs_to :salle
	has_many :incidents
	
	validates_associated :salle, :pdms
	validates_presence_of :nom_machine, :connectivite, :salle_id
	validates_uniqueness_of :nom_machine, :adresse_ip

	def display_connect
		if(connectivite == 0)
			return "Deconnecte"
		else
			return "Connecte"
		end
	end
	
	def totalpc
		inc = Incident.all(:conditions =>{:machine_id => id})
		count = 0
		inc.each do |i|
			count = count + i.delais
		end
		return count
	end
	
	
	
  
  def urgency 
    @urgencies = Incident.count( :all, :conditions => { :date_resolution => nil, :machine_id => self.id })
    if (@urgencies != 0 )
      @urgent = 'urgent'
      @urgent
    else
      @urgent = 'no_urgent'
      @urgent
    end
  end
	def tpsop
		pc = Incident.all(:conditions =>{:machine_id => id})
		
		return (-(created_at - DateTime.now) - totalpc)/86400

	
	
	end
	
	# def validate_ip_address
		# reg = /[0-9]{3}.[0-9]{3}.[0-9]{3}.[0-9]{3}/ 
		# return (reg.match(machine_id.to_s))? true : false
	# end
	
end
